package com.boil.qz.safekavass.model;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by ChenYong on 2017-07-11.
 * <br>
 * 角色实体类。
 */
@Entity
@Table(name = "TB_QZ_ROLE")
public class RoleEntity implements Serializable {
    /**
     * 序列化 ID
     */
    private static final long serialVersionUID = 2629619332920805476L;
    /**
     * 角色 ID
     */
    @Id
    @SequenceGenerator(name = "SgQzRole", sequenceName = "SE_QZ_ROLE", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SgQzRole")
    private long id;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色代码
     */
    private String roleCode;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 角色拥有的资源 <code>Set</code>
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, mappedBy = "roles")
    private Set<ResourceEntity> resources;
    /**
     * 角色拥有的用户 <code>Set</code>
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, mappedBy = "roles")
    private Set<LoginUser> loginUsers;

    // 普通初始化块
    {
        resources = new TreeSet<ResourceEntity>();
        loginUsers = new HashSet<LoginUser>();
    }

    /**
     * 默认构造器。
     */
    public RoleEntity() {
        super();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleCode() {
        return roleCode;
    }

    public void setRoleCode(String roleCode) {
        this.roleCode = roleCode;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Set<ResourceEntity> getResources() {
        return resources;
    }

    public void setResources(Set<ResourceEntity> resources) {
        this.resources = new TreeSet<ResourceEntity>(resources);
    }

    public Set<LoginUser> getLoginUsers() {
        return loginUsers;
    }

    public void setLoginUsers(Set<LoginUser> loginUsers) {
        this.loginUsers = loginUsers;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RoleEntity that = (RoleEntity) o;

        return id == that.id;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("RoleEntity{");
        sb.append("id=").append(id);
        sb.append(", roleName='").append(roleName).append('\'');
        sb.append(", roleCode='").append(roleCode).append('\'');
        sb.append(", createTime=").append(createTime);
        sb.append(", resources=").append(resources);
        sb.append(", loginUsers=").append(loginUsers);
        sb.append('}');
        return sb.toString();
    }
}